<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h3>编辑框中的文字选择</h3>
		<form>
			<input type='text' name="username" value="输入用户名">
			<input type="button" onclick="setSelected()" value="选择编辑框中文字">
			<input type="button" onclick="getSelected()" value="获取用户选中的文字">
		</form>
		<script>
			function setSelected(){
				document.forms[0].elements["username"].select();
			}
			function getSelected(){
				var username = document.forms[0].elements["username"];
				var selected = username.value.substring(username.selectionStart, 
					username.selectionEnd);
				alert(selected);
			}
		</script>
		
		<h3>限制编辑框中的文字输入</h3>
		<form>
			<input type='text' name="username" placeholder="输入数字" onkeypress="checkInput()">
		</form>
		<script type="text/javascript" src="js/eventUtil.js" ></script>
		<script>
			function checkInput(event){
				event = EventUtil.getEvent(event);
				var charCode = EventUtil.getCharCode(event);
				var reg = /\d/;
				if(!reg.test(String.fromCharCode(charCode))){
					EventUtil.preventDefault(event);			// 不显示该字符
				}
			}
		</script>
		
		<h3>当编辑框失去输入焦点时判断</h3>
		<form name="form1">
			<input type='text' name="username" placeholder="输入数字" onblur="checkValue()">
			<input type='button' value="提交" />
		</form>
		<script>
			function checkValue(){
				var username = document.forms["form1"].elements["username"];
				var reg = /\d/;
				if(!reg.test(username.value)){
					alert("必须输入数字");
					username.focus();
				}
			}
		</script>
	</body>
</html>
